**type below if necessary*******************************************************
**ssc install estout
**cd "D:\Documents\Data and Dofiles\school_data"
**use "complete_data(2005-2014)"

**create graphs*****************************************************************

* compute treatment and control group average dropout rates

by year, sort: egen treatment_group=mean(dropout_rate) if treat_district == 1
by year, sort: egen control_group=mean(dropout_rate) if treat_district == 0

* create individual graphs

forval i=1/4 {
local year: word `i' of 2009 2011 2012 2013
replace treat_`i'=0 if treat_district == 0
by year, sort: egen group_`i'=mean(dropout_rate) if treat_`i' == 1
graph twoway line group_`i' year || line control_group year, lpat(dash) xline(`year', lcol(black))
graph save graph_g`i', replace 
}

graph twoway line treatment_group year || line control_group year, lpat(dash) xline(2009, lcol(black))
graph export "aggregate_graph.tif", as(tif) replace

* saved combined graphs

graph combine graph_g1.gph graph_g2.gph graph_g3.gph graph_g4.gph, ysize(15) xsize(20) ycommon col(2)
graph export "combined_graph.tif", as(tif) replace

**summary statistics************************************************************

bysort treat_dd: sum dropout_rate schooltype superintendent metropolitan teacher_ratio class_size per_pupil per_capita if treat_district == 1
bysort after_policy1: sum dropout_rate schooltype superintendent metropolitan teacher_ratio class_size per_pupil per_capita if treat_district == 0

**diff-in-diff estimates********************************************************

* 1-1 simple subtraction

bysort treat_1: ttest dropout_rate, by(after_policy1) 
bysort treat_2: ttest dropout_rate, by(after_policy2) 
bysort treat_3: ttest dropout_rate, by(after_policy3) 
bysort treat_4: ttest dropout_rate, by(after_policy4)

* 1-2 simple regression 

forval i=1/4 {
xi: reg dropout_rate i.treat_`i'*i.after_policy`i'
est store simplediff`i'
}
esttab simplediff1 simplediff2 simplediff3 simplediff4, ///
rename(_ItreXaft_1_1 treatXafter _Itreat_1_1 treat_dist _Itreat_2_1 treat_dist _Itreat_3_1 treat_dist _Itreat_4_1 treat_dist _Iafter_pol_1 after_policy) ///
cells(b(star fmt(3)) se(par fmt(4))) starlevels(+ 0.1 * 0.05 ** 0.01 *** 0.001)

* 1-3 regression (controls added)

forval i=1/4 {
xi: reg dropout_rate i.treat_`i'*i.after_policy`i' schooltype superintendent metropolitan pop faculty schools ln_educ_tax ln_gross_tax
est store diff`i'
}
esttab diff1 diff2 diff3 diff4, ///
rename(_ItreXaft_1_1 treatXafter _Itreat_1_1 treat_dist _Itreat_2_1 treat_dist _Itreat_3_1 treat_dist _Itreat_4_1 treat_dist _Iafter_pol_1 after_policy) ///
cells(b(star fmt(3)) se(par fmt(4))) starlevels(+ 0.1 * 0.05 ** 0.01 *** 0.001)

**fixed effects estimates*******************************************************

xtset id year //two dimensions of the diff-in-diff (fixed effects captured in xtreg command)

xtreg dropout_rate treat_dd schooltype superintendent metropolitan pop faculty schools ln_educ_tax ln_gross_tax, fe vce(cluster district)
est store fixed2
xtreg dropout_rate treat_dd, fe vce(cluster district)
est store fixed

esttab fixed fixed2, cells(b(star fmt(3)) se(par fmt(2))) starlevels(+ 0.1 * 0.05 ** 0.01 *** 0.001)

**random effects estimates******************************************************

xtset id year //two dimensions of the diff-in-diff (fixed effects captured in xtreg command)

xtmixed dropout_rate treat_dd schooltype superintendent metropolitan pop faculty schools ln_educ_tax ln_gross_tax, vce(cluster province) || province: superintendent
est store mixed2
xtmixed dropout_rate treat_dd, vce(cluster province) || province:
est store mixed

esttab mixed mixed2, cells(b(star fmt(3)) se(par fmt(3))) starlevels(+ 0.1 * 0.05 ** 0.01 *** 0.001)
